package com.neusoft.nepg.gridders.controller;

import com.neusoft.nepg.gridders.entity.Feedback;
import com.neusoft.nepg.gridders.entity.FinalData;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.stage.Stage;

import java.util.List;

import static com.neusoft.nepg.gridders.constants.Constants.*;
import static com.neusoft.nepg.gridders.utils.AlertUtils.showDialog;
import static com.neusoft.nepg.gridders.utils.FileUtils.ListReader;
import static com.neusoft.nepg.gridders.utils.FileUtils.ListWriter;

public class FeedbackInfoViewController {
    @FXML
    private Label userInformation;
    @FXML
    private Label location;
    @FXML
    private Label address;
    @FXML
    private Label level;
    @FXML
    private Label status;
    @FXML
    private Label realLevel;
    @FXML
    private TextField so21;
    @FXML
    private TextField so22;
    @FXML
    private TextField co;
    @FXML
    private Button commit;
    @FXML
    private Label so21Level;
    @FXML
    private Label coLevel;
    @FXML
    private Label so22Level;

    private Feedback feedback;

    public void initial(Feedback feedbacks) {
        feedback = feedbacks;
        this.userInformation.setText(feedback.getUserInformation());
        this.location.setText(feedback.getProvince() + " " + feedback.getCity());
        this.address.setText(feedback.getAddress());
        this.level.setText(feedback.getLevel());
        this.status.setText(POLLUTION_LEVEL_DESCRIPTION.get(feedback.getLevel()));

        TextFormatter<Integer> formatter = new TextFormatter<>(change -> {
            String newText = change.getControlNewText();
            if (newText.matches("\\d*")) {
                return change;
            }
            return null;
        });

        so21.setTextFormatter(formatter);
        so21.textProperty().addListener((observable, oldValue, newValue) -> {
            so21Level.setText(SO2_TO_AQI_LEVEL(Integer.parseInt(newValue)));
        });

        so22.setTextFormatter(formatter);
        so22.textProperty().addListener((observable, oldValue, newValue) -> {
            so22Level.setText(PM25_TO_AQI_LEVEL(Integer.parseInt(newValue)));
        });

        co.setTextFormatter(formatter);
        co.textProperty().addListener((observable, oldValue, newValue) -> {
            coLevel.setText(CO_TO_AQI_LEVEL(Integer.parseInt(newValue)));
        });

       so21Level.textProperty().addListener((observable, oldValue, newValue) -> updateRealLevel());
       coLevel.textProperty().addListener((observable, oldValue, newValue) -> updateRealLevel());
       so22Level.textProperty().addListener((observable, oldValue, newValue) -> updateRealLevel());
    }

    private void updateRealLevel() {

        String maxLevel = VALUE_SWITCH_CHARACTER.get(Math.max(CHARACTER_SWITCH_VALUE.get(so21.getText()),Math.max(CHARACTER_SWITCH_VALUE.get(so22Level.getText()),CHARACTER_SWITCH_VALUE.get(coLevel.getText()))));
        String description = POLLUTION_LEVEL_DESCRIPTION.get(maxLevel);
        realLevel.setText("实测AQI等级：" + maxLevel + description);
    }

    /**
     * get data from files, and write to database, commit.
     * delete the feedback file.
     * reload the
     */
    public void commit() {
        List<FinalData> finalDataList = ListReader("finalData.txt");
        //set information
        FinalData finalData = new FinalData();
        ListWriter("finalData.txt", finalDataList);
        //delete the feedback file.
        List<Feedback> feedbackList = ListReader("feedbackList.txt");
        feedbackList.remove(feedback);
        ListWriter("feedbackList.txt", feedbackList);

        showDialog("commit success!");

        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/FeedbackListView.fxml"));
            Parent root = (Parent) fxmlLoader.load();
            Scene scene = new Scene(root);
            Stage stage = (Stage) this.commit.getScene().getWindow();
            stage.setScene(scene);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
